package com.dz.controller.admin;

import com.dz.dto.CategoryDTO;
import com.dz.dto.CategoryPageQueryDTO;
import com.dz.entity.Category;
import com.dz.enumeration.AppHttpCodeEnum;
import com.dz.result.Result;
import com.dz.service.CategoryService;
import com.dz.vo.PageVo;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 鄧浩奇
 * @classname CategoryController
 * @description
 * @since 2023/12/16 21:32
 **/


@Api(tags = "分类管理接口")
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result<PageVo> page(@Valid CategoryPageQueryDTO pageQueryDTO) {
        return categoryService.page(pageQueryDTO);
    }

    @ApiOperation("新增分类")
    @PostMapping()
    public Result<String> addCategory(@RequestBody @Valid CategoryDTO categoryDTO) {
        return categoryService.addCategory(categoryDTO);
    }

    @ApiOperation("修改分类")
    @PutMapping()
    public Result<String> updateCategory(@RequestBody @Valid CategoryDTO categoryDTO) {
        return categoryService.update(categoryDTO);
    }

    @ApiOperation("启用、禁用分类")
    @PostMapping("/status/{status}")
    public Result<String> changeStatus(@PathVariable Integer status, Long id) {
        return categoryService.changeStatus(id,status);
    }

    @ApiOperation("删除分类")
    @DeleteMapping()
    public Result<String> deleteCategory(Long id) {
        return categoryService.deleteCategory(id);
    }

    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result<List<Category>> getCategoryByType(Integer type) {
        Assert.notNull(type, AppHttpCodeEnum.DATA_ERROR.getMsg());
        List<Category> list =  categoryService.getListByType(type);
        return Result.success(list);
    }

}
